<?php
/**
 * User: 魔十七
 * Date: 2020/11/11
 * Time: 8:30
 */

namespace Samecity\Model;

use Think\Model;
use Samecity\Model\MerchantModel;
use Samecity\Model\GoodsModel;

class ShopCartModel extends Model
{
    private $MerchantModel;
    private $GoodsModel;

    protected function _initialize()
    {
        $this->MerchantModel = new \Samecity\Model\MerchantModel();
        $this->GoodsModel = new \Samecity\Model\GoodsModel();
    }


    /**
     * 购物车列表
     * @param $openId   用户openid
     */
    public function getShopCartList($openId)
    {
        $shopCart = M('shop_cart')->field('samecity_shop_cart.shop_id,samecity_merchant.id,samecity_merchant.title')
            ->join('samecity_merchant ON samecity_shop_cart.shop_id=samecity_merchant.id')
            ->distinct(true)->order('samecity_shop_cart.shop_id desc')->select(); /* 店铺名称 */
        foreach($shopCart as $key => $list){
            $item['shop_id'] =  $list['shop_id'];
            $shopCartWhere['shop_id'] = $list['id'];
            $shopCartArr = M('shop_cart')->field('cart_id,goods_id,spec,nums')->where($shopCartWhere)->select(); /* 购物车信息 */
            foreach ($shopCartArr as $keys => $value){/* 商品详情 */
                $item['cart_id']   = $value['cart_id'];
                $item['goods_id']   = $value['goods_id'];
                $item['spec']   = $value['spec'];
                $item['goods_nums']   = $value['nums'];
                $goodsWhere['shop_id'] = $list['id'];
                $goodsWhere['id'] = $value['goods_id'];
                $goods  = M('goods')->field('id,title,new_price,old_price,thumb,m_pic,inventory,postage,inputtime')->where($goodsWhere)->select();
                $item['goods'][$keys]  = $goods;
            }
            $data[$key] = $item;
        }
        return array(
            'code'  => 1001,
            'msg'   => '返回成功',
            'data'  => $data
        );
    }



    /**
     * 购物车添加
     * @param   $open_id    用户openID
     * @param   $cart_type       暂时不用 购物车类型(1:普通类型 2:秒杀)
     * @param   $shop_id    商铺ID
     * @param   $goods_id   商品ID
     * @param   $spec       商品规格(json)
     * @param   $nums       商品数量
     * @param   $type       数量类型(1添加  2赋加)
     */
    public function getCartAdd($open_id,$shop_id,$goods_id,$spec,$nums,$type)
    {
        /* 商铺是否正常 */
        $MerchantInfo   = $this->MerchantModel->getIsMerchant($shop_id);

        if ($MerchantInfo = '') {
            return $this->getError(array('code'=>'1002','msg'=>"商铺已休息,或已关闭!"));
        }
        /* 商品是否上架 */
        $GoodsInfo = $this->GoodsModel->getIsGoods($goods_id);
        if ($GoodsInfo = '') {
            return $this->getError(array('code'=>'1002','msg'=>"该商品已下架或已售空!"));

        }
        $canBuyNum = $GoodsInfo['inventory'];

        $where['openid']    = $open_id;
        $where['shop_id']   = $shop_id;
        $where['goods_id']  = $goods_id;
        $cat_info = $this->where($where)->find();

        if ($cat_info){
            if ($type == 1){
                $cat_info['nums'] = $nums + $cat_info['nums'];
            }else{
                $cat_info['nums'] = $nums;
            }
            if ($cat_info['nums'] > $canBuyNum) {
                $result['code'] = 1002;
                $result['msg'] = '库存不足';
            }
            $w['cart_id'] = $cat_info['cart_id'];

            $result['data'] = M('shop_cart')->where($w)->save($cat_info);
            $result['msg']      = '追加购物车成功';
        } else {
            if ($nums > $canBuyNum) {
                $result['code'] = 1002;
                $result['msg'] = '库存不足';
            }
            $data['openid']     = $open_id;
            $data['shop_id']    = $shop_id;
            $data['goods_id']   = $goods_id;
            $data['spec']       = $spec;
            $data['nums']       = $nums;
            $result['data']     = M('shop_cart')->add($data);

            $result['msg']      = '添加购物车成功';
        }
        $result['code']     = 1001;
        return array(
            'code'  => $result['code'],
            'msg'   => $result['msg'],
            'data'  => $result['data']
        );
    }

    /**
     * 购物车删除
     * @param $cart_id  购物车Id
     * @param $open_id  用户ID
     */
    public function getCartDelete($open_id,$cart_id)
    {
        $where['open_id'] = $open_id;
        $ShopCartDel = M('shop_cart')->where($where)->delete($cart_id);
        if ($ShopCartDel){
            $data = array(
                'code' => '1001',
                'msg'  => '已将商品移除购物车',
                'data' => $ShopCartDel
            );
        }else{
            $data = array(
                'code' => '1002',
                'msg'  => '移除购物车失败,请检查网络...',
                'data' => $ShopCartDel
            );
        }
        return $data;
    }

}
